﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IntelligentCMS
{
    public partial class CMSSave : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        

        public string Operation
        {
            get
            {
                return operation;
            }
            set
            {

                operation = value;
            }


        }

        public List<iCMSControl> TheControls
        {
            get
            {
                return theControls;
            }

            set
            {
                theControls = value;
            }


        }

        // Who is posting this?
        private string userid = string.Empty;
        // Where should we send them when they click Save?
        private string url = string.Empty;
        // What kind of operation do we want when they click Save?
        private string operation = string.Empty;

        public List<iCMSControl> theControls = new List<iCMSControl>();


        protected void Button_Save_Click(object sender, EventArgs e)
        {
            List<string> errors = new List<string>();

            foreach(iCMSControl c in theControls) {
                errors.AddRange(c.Errors);
            }

            if (errors.Count > 0)
            {
                theCMSErrorBox.Errors = errors;
                return;
            }

            

            switch (operation)
            {
                case CMSSave.OPERATION_Insert:
                    Insert();
                    break;
                case CMSSave.OPERATION_Update:
                    Update();
                    break;
                case CMSSave.OPERATION_None:
                    break;
                default:
                    break;

            }

        }

        public const string OPERATION_None = "None";
        public const string OPERATION_Insert = "Insert";
        public const string OPERATION_Update = "Update";

        private long UserID = -1;

       

        // Update operation -> update an existing record.
        private void Update()
        {
            string table = "Profile";

            string sqlquery = string.Empty;
            sqlquery += "UPDATE [" + table + "] SET ";


            List<Pair> sqlValues = new List<Pair>();

            foreach (iCMSControl c in theControls)
            {
                sqlValues.AddRange(c.GetSqlValues);
            }


            bool first = true;
            foreach (Pair p in sqlValues)
            {
                if (first)
                {
                    sqlquery += "[" + p.Key + "]='" + p.Value + "'";
                    first = false;
                }
                else
                {
                    sqlquery += ",[" + p.Key + "]='" + p.Value + "'";
                }

            }

            sqlquery += " WHERE [UserID] = " + UserID;
        }



        // Insert operation -> inserting a new record.
        private void Insert()
        {
            string table = string.Empty;

            string sqlquery = string.Empty;
            sqlquery += "INSERT INTO [" + table + "] (";


            List<Pair> sqlValues = new List<Pair>();

            foreach (iCMSControl c in theControls)
            {
                sqlValues.AddRange(c.GetSqlValues);
            }


            bool first = true;
            foreach (Pair p in sqlValues)
            {
                if (first)
                {
                    sqlquery += "[" + p.Key + "]";
                    first = false;
                }
                else
                {
                    sqlquery += ",[" + p.Key + "]";
                }

            }

            sqlquery += ") VALUES (";

            first = true;
            foreach (Pair p in sqlValues)
            {
                if (first)
                {
                    sqlquery += "'" + p.Value + "'";

                }
                else
                {
                    sqlquery += ",'" + p.Value + "'";
                }

            }

            sqlquery += ")";





        }
    }
}